﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;

public class FolkLoader : MonoBehaviour
{
    public GameObject ShiftObj = null;
    public GameObject LiftObj = null;

    float InitLiftFolkYDistance;

    bool upTriger = false;
    bool downTriger = false;
    private float moveSpeed;    //叉车前后移动速度

    //float liftTraY = 0;
    // Start is called before the first frame update
    void Start()
    {
        //判断物体是否拉入参数栏,没有将尝试自动填入
        if (ShiftObj == null)
        {
            //Debug.LogError("参数栏中不能有缺省项");
            //自动将物体加载到参数栏
            ShiftObj = transform.Find("body").transform.Find("gear shifter").gameObject;
        }
        if (LiftObj == null)
        {
            LiftObj = transform.Find("lift").gameObject;
        }
        //LiftTra = LiftObj.transform;
        InitLiftFolkYDistance = LiftObj.transform.position.y - transform.position.y;
        moveSpeed = 1.0f;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.P) && (LiftObj.transform.position.y - transform.position.y) < (InitLiftFolkYDistance + 1.5f))
        {
            //do sth
            LiftObj.transform.Translate(Vector3.up * 0.5f * Time.deltaTime);
            if (upTriger == false)
            {
                ShiftObj.transform.Rotate(Vector3.left * 15f);
            }
            upTriger = true;
        }
        else
        {
            if (upTriger == true)
            {
                ShiftObj.transform.Rotate(Vector3.left * (-15f));
            }
            LiftObj.transform.Translate(Vector3.up * 0);
            upTriger = false;
        }

        if (Input.GetKey(KeyCode.L) && (LiftObj.transform.position.y - transform.position.y) > InitLiftFolkYDistance)
        {
            //do sth
            LiftObj.transform.Translate(Vector3.up * -0.5f * Time.deltaTime);
            if (downTriger == false)
            {
                ShiftObj.transform.Rotate(Vector3.left * (-15f));
            }
            downTriger = true;
        }
        else
        {
            if (downTriger == true)
            {
                ShiftObj.transform.Rotate(Vector3.left * 15f);
            }
            LiftObj.transform.Translate(Vector3.up * 0);
            downTriger = false;
        }
        //Input.GetAxis()获取遥杆纵向轴数据(-1~1中间)
        float translation = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
        //前后移动叉车
        transform.Translate(transform.forward * translation);

        //旋转叉车
        float rotation = Input.GetAxis("Horizontal") * 20f * Time.deltaTime;
        transform.Rotate(Vector3.up * rotation);
    }
}
